<?php

namespace app\crm\model;

use app\admin\model\User;
use app\common\traits\ActionRecordTrait;
use think\facade\Db;
use app\admin\model\Common;
use think\response\Json;
use think\Validate;
use app\crm\logic\WhereLogic;

use app\admin\controller\ApiCommon;
use app\crm\model\Setting;
use app\crm\model\SettingCategory;
use Kingdee\Kingdee;

class ReserveandLost extends Common
{
    use ActionRecordTrait;
    /**
     * 为了数据库的整洁，同时又不影响Model和Controller的名称
     * 我们约定每个模块的数据表都加上相同的前缀，比如CRM模块用crm作为数据表前缀
     */
    protected $name = 'customer';

    // 成交顾客
    public function cj_customer($shopIds,$request)
    {


        $m_where = [];

        array_push($m_where,['shop_type','=','1']);

        array_push($m_where,['type','=','2']);

        array_push($m_where,['status','=','1']);

        $mShopIds = Db::name('shop')->where($m_where)->column('shop_id');

        $where = [];

        $shop_id = $request['shop_id'] ?? "";

        // var_dump($shop_id);
        // exit;

        if($shop_id){

            array_push($where,['c.shop_id','=',$shop_id]);

        }

        $cj_type = $request['cj_type'] ?? '0';


        if($cj_type == '1'){

            $tagTime = '0';

        }else{

            $tagTime = strtotime('-1 years');

        }

        // echo $tagTime;
        // exit;

        array_push($where,['cj_time','>',$tagTime]);
        array_push($where,['cj_money','>=','500.00']);

        array_push($where,['c.final_consumption|c.final_service','>','0']);

        array_push($where,['c.shop_id','in',$shopIds]);

        array_push($where,['c.shop_id','not in',$mShopIds]);

        $cj_time = $request['cj_time'] ?? '';

        if($cj_time){

            $cj_time_str = strtotime($cj_time);

            array_push($where,['c.cj_time','>=',$cj_time_str]);

        }

        $customer_name = $request['customer_name'] ?? "";

        if($customer_name){

            array_push($where,['c.customer_name','like',$customer_name."%"]);

        }



        $member_classification = $request['member_classification'] ?? '';

        if($member_classification){

            array_push($where,['c.member_classification','=',$member_classification]);

        }

        $telephone = $request['telephone'] ?? "";

        if($telephone){

            array_push($where,['c.telephone','=',$telephone]);

        }

        $member_classification = $request['member_classification'] ?? "";

        if($member_classification){

            array_push($where,['c.member_classification','=',$member_classification]);

        }

        $scar_id = $request['scar_id'] ?? "";

        if($scar_id){

            // array_push($where,['cs.customer_setting_id_type','like',"%".$scar_id."%"]);

            array_push($where,['cs.customer_setting_id_type','=',$scar_id]);

        }

        $last_kftime_start = $request['last_kftime_start'] ?? "";
        $last_kftime_end = $request['last_kftime_end'] ?? "";

        if($last_kftime_start){

            $last_kftime_start1 = strtotime($last_kftime_start);

            array_push($where,['c.last_kftime','>=',$last_kftime_start1]);

        }

        if($last_kftime_end){

            $last_kftime_end1 = strtotime($last_kftime_end.' 23:59:59');

            array_push($where,['c.last_kftime','<=',$last_kftime_end1]);

        }

        $nurse_id = $request['nurse_id'] ?? '';

        if($nurse_id){

            array_push($where,['c.nurse_id','=',$nurse_id]);

        }
        // echo "<pre>";
        // print_r($where);
        // echo "</pre>";
        // exit;

        $cjCustomerIds = Db::name('customer')->alias('c')
                        ->join('customer_scar cs', 'cs.customer_id=c.id', 'LEFT')
                        ->where($where)
                        ->group('c.id')
                        ->column('c.id');


        // echo "<pre>";
        // print_r($cjCustomerIds);
        // echo "</pre>";
        // exit;

        $lost_caseIds = $this->lost_case($cjCustomerIds,date('Y-m-d'));


        $cjCustomerIds = array_diff($cjCustomerIds,$lost_caseIds);

        return $cjCustomerIds;

    }

    // 保有顾客
    public function baoyou_customer($ids,$date)
    {

        $tagTime = strtotime($date.' -3 month');

        $where = [];

        array_push($where,['id','in',$ids]);
        array_push($where,['final_consumption|final_service','>',$tagTime]);

        $customer_list = Db::name('customer')->where($where)->column('id');

        return $customer_list;

    }

    public function shop_baoyou_customer($ids,$date)
    {

        $tagTime = strtotime($date.' -3 month');

        $where = [];

        array_push($where,['id','in',$ids]);
        array_push($where,['final_consumption|final_service','>',$tagTime]);

        $customer_list = Db::name('customer')->where($where)->group('shop_id')->column('count(id) as sum,shop_id');

        // return count($customer_list);
        return $customer_list;

    }

    // 流失预警顾客
    public function warning_customer($ids,$date,$type,$shop_id,$start_time)
    {
        // echo "<pre>";
        // print_r($ids);
        // echo "</pre>";

        // echo "<pre>";
        // print_r($shop_id);
        // echo "</pre>";

        // echo $date;echo "<br/>";
        // echo $type;echo "<br/>";
        // echo $start_time;echo "<br/>";
        // exit;


        $type = $type ?? '';

        // var_dump($type);

        // exit;
        if($type == 'back'){ // 月初流失预警

            $date = date('Ymd',strtotime($start_time));

            $where = [];

            array_push($where,['shop_id','in',$shop_id]);

            array_push($where,['date','=',$date]);

            $warn_customerIds = Db::name('customer_tj')->where($where)->column('warn_customer_ids');

            // echo "<pre>";
            // print_r($warn_customerIds);
            // echo "</pre>";
            // exit;
            if($warn_customerIds){

                $warn_customer_ids = implode(',',$warn_customerIds);
                $customer_list = explode(',',$warn_customer_ids);

            }else{
                $customer_list = [];
            }




            // var_dump($shop_id)."<br/>";
            // echo $start_time."<br/>";
            // exit;
        }else{

        // if(!$type){

            $tagTime = strtotime($date.' -30 day');

            $startTime = strtotime($date.' -90 day');

            $where = [];

            array_push($where,['id','in',$ids]);

            // array_push($where,['final_consumption','>=',$startTime]);
            array_push($where,['final_consumption','<',$tagTime]);

            // array_push($where,['final_service','>=',$startTime]);
            array_push($where,['final_service','<',$tagTime]);

            array_push($where,['final_consumption|final_service','between',"$startTime,$tagTime"]);
            array_push($where,['final_consumption|final_service','>',$startTime]);


            // echo "<pre>";
            // print_r($where);
            // echo "</pre>";
            // exit;

            // array_push($where,['member_label','not like','%588%']);

            // array_push($where,['member_label','not like','%589%']);

            $customer_list = Db::name('customer')->where($where)->column('id');

        }


        // return count($customer_list);
        return $customer_list;

    }

    // 流失顾客
    public function lost_customer($ids,$date)
    {

        $where = [];

        array_push($where,['customer_id','in',$ids]);

        array_push($where,['selection_results','=',2]);

        $youan_customer = Db::name('case')->where($where)->column('customer_id');

        $customer_ids = array_diff($ids,$youan_customer);

        $tagTime = strtotime($date.' -3 month');

        $startTime = '0';

        $where = [];

        array_push($where,['id','in',$customer_ids]);

        array_push($where,['final_consumption','>=',$startTime]);
        array_push($where,['final_consumption','<',$tagTime]);

        array_push($where,['final_service','>=',$startTime]);
        array_push($where,['final_service','<',$tagTime]);

        array_push($where,['final_consumption|final_service','>',$startTime]);

        // array_push($where,['member_label','not like','%588%']);

        // array_push($where,['member_label','not like','%589%']);

        $customer_list = Db::name('customer')->where($where)->column('id');

        // return count($customer_list);
        return $customer_list;

    }

    // 流失顾客
    public function shop_lost_customer($ids,$date)
    {

        $where = [];

        array_push($where,['customer_id','in',$ids]);

        array_push($where,['selection_results','=',2]);

        $youan_customer = Db::name('case')->where($where)->column('customer_id');

        $customer_ids = array_diff($ids,$youan_customer);

        $tagTime = strtotime($date.' -90 day');

        $startTime = '0';

        $where = [];

        array_push($where,['id','in',$customer_ids]);

        array_push($where,['final_consumption','>=',$startTime]);
        array_push($where,['final_consumption','<',$tagTime]);

        array_push($where,['final_service','>=',$startTime]);
        array_push($where,['final_service','<',$tagTime]);

        array_push($where,['final_consumption|final_service','>',$startTime]);

        // array_push($where,['member_label','not like','%588%']);

        // array_push($where,['member_label','not like','%589%']);

        $customer_list = Db::name('customer')->where($where)->group('shop_id')->column('count(id) as sum,shop_id');

        // return count($customer_list);
        return $customer_list;

    }

    // 流失优案
    public function lost_case($ids,$date){

        $tagTime = strtotime($date.' -90 day');

        $startTime = '0';

        $where = [];

        array_push($where,['customer_id','in',$ids]);

        array_push($where,['selection_results','=',2]);

        $youan_customer = Db::name('case')->where($where)->column('customer_id');

        $where = [];

        array_push($where,['id','in',$youan_customer]);

        array_push($where,['final_consumption','>=',$startTime]);
        array_push($where,['final_consumption','<',$tagTime]);

        array_push($where,['final_service','>=',$startTime]);
        array_push($where,['final_service','<',$tagTime]);

        array_push($where,['final_consumption|final_service','>',$startTime]);

        // array_push($where,['member_label','not like','%588%']);

        // array_push($where,['member_label','not like','%589%']);

        $customer_list = Db::name('customer')->where($where)->column('id');

        // return count($customer_list);
        return $customer_list;

    }

    // 标记优案
    public function tag_customer($ids)
    {

        $where  = [];
        $where1 = [];
        $where2 = [];

        array_push($where,['id','in',$ids]);
        // array_push($where1,['member_label','like','%625%']);
        // array_push($where2,['id','in',$ids]);
        // array_push($where2,['member_label','like','%626%']);

        $tag1 = '625';
        $tag2 = '626';


        $customer_list = Db::name('customer')
                        ->where($where)
                        ->where(function ($query) use ($tag1,$tag2) {

                            $tag_where1 = [];
                            $tag_where2 = [];

                            array_push($tag_where1,['member_label','like',"%".$tag1."%"]);

                            array_push($tag_where2,['member_label','like',"%".$tag2."%"]);

                            $query->where(function ($query) use ($tag_where1){
                                $query->where($tag_where1);
                            });

                            $query->whereOr(function ($query) use ($tag_where2){
                                $query->where($tag_where2);
                            });

                        })
                        ->column('id');

        // echo "<pre>";
        // print_r($customer_list);
        // echo "</pre>";
        // exit;

        return $customer_list;

    }

    public function back_customer($ids,$tag_time,$over_time)
    {
        $tag_time = strtotime($tag_time);

        // 收款单
        $where = [];

        array_push($where,['uid','in',$ids]);

        array_push($where,['status','=','2']);

        array_push($where,['type','=','3']);

        array_push($where,['refund_type','=','101.02']);

        array_push($where,['deal_time','between',"$tag_time,$over_time"]);

        $sk_ids = Db::name('shop_refund_order')->where($where)->column('uid');

        // 销售出库单
        $where = [];

        array_push($where,['uid','in',$ids]);

        array_push($where,['status','=','2']);

        array_push($where,['deal_time','between',"$tag_time,$over_time"]);

        $xs_ids = Db::name('shop_order')->where($where)->column('uid');

        // 服务记录
        $where = [];

        array_push($where,['customer_id','in',$ids]);

        array_push($where,['create_time','between',"$tag_time,$over_time"]);

        $fw_ids = Db::name('service_log')->where($where)->column('customer_id');

        $customer_list = [];

        foreach ($sk_ids as $key => $value) {

            if(in_array($value,$customer_list)){

            }else{
                array_push($customer_list,$value);
            }

        }

        foreach ($xs_ids as $key => $value) {

            if(in_array($value,$customer_list)){

            }else{
                array_push($customer_list,$value);
            }

        }

        foreach ($fw_ids as $key => $value) {

            if(in_array($value,$customer_list)){

            }else{
                array_push($customer_list,$value);
            }

        }

        return $customer_list;

    }


}
